package com.chonor.mall.service.impl;

import com.chonor.mall.dao.ProductCategoryAttributeDao;
import com.chonor.mall.dao.ProductCategoryDao;
import com.chonor.mall.dto.ProductCategoryParam;
import com.chonor.mall.dto.ProductCategoryWithChildrenItem;
import com.chonor.mall.mapper.ProductCategoryAttributeMapper;
import com.chonor.mall.mapper.ProductCategoryMapper;
import com.chonor.mall.mapper.ProductMapper;
import com.chonor.mall.model.*;
import com.chonor.mall.service.ProductCategoryService;
import com.chonor.mall.vo.ProductCategoryVO;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductCategoryAttributeDao productCategoryAttributeDao;

    @Autowired
    private ProductCategoryAttributeMapper productCategoryAttributeMapper;

    @Autowired
    private ProductCategoryDao productCategoryDao;

    @Override
    public int create(ProductCategoryParam productCategoryParam) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setProductCount(0);
        productCategory.setShowStatus(1);
        BeanUtils.copyProperties(productCategoryParam, productCategory);
        //没有父分类时为一级分类
        setCategoryLevel(productCategory);
        int count = productCategoryMapper.insertSelective(productCategory);
//        //创建筛选属性关联
//        List<Long> productAttributeIdList = productCategoryParam.getProductAttributeIdList();
//        if(!CollectionUtils.isEmpty(productAttributeIdList)){
//            insertRelationList(productCategory.getId(), productAttributeIdList);
//        }
        return count;
    }

    /**
     * 批量插入商品分类与筛选属性关系表
     * @param productCategoryId 商品分类id
     * @param productAttributeIdList 相关商品筛选属性id集合
     */
    private void insertRelationList(Long productCategoryId, List<Long> productAttributeIdList) {
        List<ProductCategoryAttribute> relationList = new ArrayList<>();
        for (Long productAttrId : productAttributeIdList) {
            ProductCategoryAttribute relation = new ProductCategoryAttribute();
            relation.setProductAttributeId(productAttrId);
            relation.setProductCategoryId(productCategoryId);
            relationList.add(relation);
        }
        productCategoryAttributeDao.insertList(relationList);
    }

    @Override
    public int update(Long id, ProductCategoryParam productCategoryParam) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        BeanUtils.copyProperties(productCategoryParam, productCategory);
        setCategoryLevel(productCategory);
        //更新商品分类时要更新商品中的名称
        Product product = new Product();
        product.setProductCategoryName(productCategory.getName());
        ProductExample example = new ProductExample();
        example.createCriteria().andProductCategoryIdEqualTo(id);
        productMapper.updateByExampleSelective(product,example);
        //同时更新筛选属性的信息
//        if(!CollectionUtils.isEmpty(productCategoryParam.getProductAttributeIdList())){
//            ProductCategoryAttributeExample relationExample = new ProductCategoryAttributeExample();
//            relationExample.createCriteria().andProductCategoryIdEqualTo(id);
//            productCategoryAttributeMapper.deleteByExample(relationExample);
//            insertRelationList(id,productCategoryParam.getProductAttributeIdList());
//        }else{
//            ProductCategoryAttributeExample relationExample = new ProductCategoryAttributeExample();
//            relationExample.createCriteria().andProductCategoryIdEqualTo(id);
//            productCategoryAttributeMapper.deleteByExample(relationExample);
//        }
        return productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }

    @Override
    public List<ProductCategory> getList(Long parentId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        ProductCategoryExample example = new ProductCategoryExample();
        example.setOrderByClause("sort desc");
        example.createCriteria().andParentIdEqualTo(parentId);
        return productCategoryMapper.selectByExample(example);
    }

    @Override
    public List<ProductCategory> getParent() {
        ProductCategoryExample example = new ProductCategoryExample();
        example.createCriteria().andParentIdEqualTo(0L);
        return productCategoryMapper.selectByExample(example);
    }

    @Override
    public List<ProductCategory> list(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        ProductCategoryExample example = new ProductCategoryExample();
        List<ProductCategory> productCategories = productCategoryMapper.selectByExample(example);
//        if(productCategories.size() > 0){
//            List<ProductCategoryVO> productCategoryVOS = new ArrayList<>(productCategories.size());
//            for (ProductCategory p:productCategories) {
//                ProductCategoryVO productCategoryVO = convertProductCategory(p);
//                productCategoryVOS.add(productCategoryVO);
//            }
//            return productCategoryVOS;
//        }
        return productCategories;
    }

    @Override
    public List<ProductCategory> listAll() {
        return productCategoryMapper.selectByExample(new ProductCategoryExample());
    }

    private ProductCategoryVO convertProductCategory(ProductCategory source){
        ProductCategoryVO target = new ProductCategoryVO();

        BeanUtils.copyProperties(source,target);
        if(!Objects.isNull(source.getParentId())){
            ProductCategory item = getItem(source.getParentId());
            target.setParent(item.getName());
        }

        return target;
    }

    @Override
    public int delete(Long id) {
        return productCategoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ProductCategory getItem(Long id) {
        return productCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateNavStatus(List<Long> ids, Integer navStatus) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setNavStatus(navStatus);
        ProductCategoryExample example = new ProductCategoryExample();
        example.createCriteria().andIdIn(ids);
        return productCategoryMapper.updateByExampleSelective(productCategory, example);
    }

    @Override
    public int showStatus(Long id, Integer showStatus) {
        ProductCategory item = getItem(id);
        item.setShowStatus(showStatus);
        int count = productCategoryMapper.updateByPrimaryKeySelective(item);
        return count;
    }

    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setShowStatus(showStatus);
        ProductCategoryExample example = new ProductCategoryExample();
        example.createCriteria().andIdIn(ids);
        return productCategoryMapper.updateByExampleSelective(productCategory, example);
    }

    @Override
    public List<ProductCategoryWithChildrenItem> listWithChildren() {
        return productCategoryDao.listWithChildren();
    }

    /**
     * 根据分类的parentId设置分类的level
     */
    private void setCategoryLevel(ProductCategory productCategory) {
        //没有父分类时为一级分类
        if (Objects.isNull(productCategory.getParentId()) || productCategory.getParentId() == 0) {
            productCategory.setLevel(0);
        } else {
            //有父分类时选择根据父分类level设置
            ProductCategory parentCategory = productCategoryMapper.selectByPrimaryKey(productCategory.getParentId());
            if (parentCategory != null) {
                productCategory.setLevel(parentCategory.getLevel() + 1);
            } else {
                productCategory.setLevel(0);
            }
        }
    }
}
